﻿//using GoogleMobileAds.Api;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RewardedVideoButton : MonoBehaviour
{
    private const string ACTION_NAME = "rewarded_video";

    private void Start()
    {

#if UNITY_ANDROID || UNITY_IOS
        if (AdmobController.instance.rewardBasedVideo != null)
        {
            AdmobController.instance.rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        }
#endif
    }

    public void OnClick()
    {
        //if (IsAvailableToShow())
        //{
        //    AdmobController.instance.ShowRewardBasedVideo();
        //}
        //else
        //{
        //    Toast.instance.ShowMessage("Ad is not available now, please wait..");
        //}

        Sound.instance.PlayButton();
        string videoAdid = VideData.Instance.GetVide();
        TTSDK.TTRewardedVideoAd r = AdMgr.Instance.ShowVideoAdNew(videoAdid);
        //创建激励视频关闭回调
        r.OnClose += (bol, val) =>
        {
            Debug.Log("关闭广告 ，是否已经看完" + bol);
            //看完广告获得提示
            if (bol)
            {

                CurrencyController.CreditBalance(3);
                MaidianNew.MaidianBackCallback(" ");
                Debug.Log("广告看完");
            }
            else
            {
                Debug.Log("广告未看完");
            }
        };

        //创建报错回调  
        r.OnError += (code, errorMessage) =>
        {

            Debug.Log("code:" + code + "错误信息：" + errorMessage);
        };

        //展示广告（新版本必须调用，不然无法展示出来广告）
        r.Show();
    }

    //public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    //{

    //}

    //public bool IsAvailableToShow()
    //{
    //    return IsActionAvailable() && IsAdAvailable();
    //}

    private bool IsActionAvailable()
    {
        return CUtils.IsActionAvailable(ACTION_NAME, ConfigController.Config.rewardedVideoPeriod);
    }

    //private bool IsAdAvailable()
    //{
    //    if (AdmobController.instance.rewardBasedVideo == null) return false;
    //    bool isLoaded = AdmobController.instance.rewardBasedVideo.IsLoaded();
    //    return isLoaded;
    //}

    private void OnDestroy()
    {
#if UNITY_ANDROID || UNITY_IOS
        if (AdmobController.instance.rewardBasedVideo != null)
        {
            AdmobController.instance.rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
        }
#endif
    }
}
